import math
import constants

class Force:
    falloff = None



    """
        source = source of force
        intensity = how strong, negative value  means push away
        falloff = The function for determaining the falloff of the force
    """
    def __init__(self,source, intensity, falloff=None):
        self.source = source
        self.intensity = intensity
        self.active = True

        if(falloff != None):
            self.falloff = falloff
        else:
            self.falloff = self.falloff_exp            
    def get_force(self, location):
        force = self.intensity
        if(self.active and self.falloff != None):
            force = self.falloff(self.source,self.intensity, location)
        else:
            return self.active

        return force

    def falloff_exp(self, source, intensity, location):
        vec = (source[0] - location[0], source[1] - location[1])
        length = math.sqrt(math.pow(vec[0],2) + math.pow(vec[1], 2))
        if(length < 10 or length > 500):
            return None

        if(intensity < 0):
            vec[0] *= -1
            vec[1] *= -1
            intensity *= -1

        normalized = (vec[0] / length, vec[1] / length)


        if(length > 0):
            force = constants.FORCE_K * intensity * math.pow(1 / length,2)
        else:
            return (normalized[0]*intensity, normalized[1]*intensity)

        falloff = (normalized[0]*force,normalized[1]*force)
        return falloff

if __name__ == '__main__':
    force = Force((0,0), 200)

    print(force.get_force((50,50)))


    






  